package com.hzc.springchan.strategy.requestlog.impl;

import com.hzc.springchan.constant.System;
import com.hzc.springchan.strategy.requestlog.IRequestLogStrategy;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Service;

/**
 * GET类型请求参数日志打印策略类
 * @author chen990627
 * @date 2022/10/18
 */
@Service(System.RequestLogStrategy.GET)
public class GetRequestLogStrategy implements IRequestLogStrategy {

    @Override
    public void requestLog(JoinPoint joinPoint) {
        // key -> value 对应
        Object[] values = joinPoint.getArgs();
        String[] argNames = ((MethodSignature) joinPoint.getSignature()).getParameterNames();
        StringBuilder requestBuilder = new StringBuilder("GET: {");
        int len = argNames.length;
        for (int idx = 0; idx < len; idx++) {
            requestBuilder.append(argNames[idx]).append(":").append(values[idx]);
            if (idx != len - 1) {
                requestBuilder.append(", ");
            }
        }
        requestBuilder.append("}");
        log(requestBuilder.toString());
    }
}
